//
//  LBMMainViewController.swift
//  微博
//
//  Created by 刘保满 on 16/5/19.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class LBMMainViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        //添加子控制器
        addChildViewController()
        
        //自定义的tabbar
        let mainTabbar = LBMMainTabbar()
        
        mainTabbar.plusBtn.addTarget(self, action: #selector(LBMMainViewController.plusBtnDidClick), forControlEvents: .TouchUpInside)
        
        
        //        self.tabBar = mainTabbar
        
        //        print(tabBar.classForCoder)
        
        //用KVC间接赋值
        setValue(mainTabbar, forKey: "tabBar")
        
        //        print(tabBar.classForCoder)
        
        
    }
    
    
    @objc  private func plusBtnDidClick() {
        
        print("加号按钮-------你点我了")
        let composeView = LBMComposeView()
        
        //        let window = UIApplication.sharedApplication().keyWindow
        //        window?.addSubview(composeView)
        //调用方法,把自己传过去
        composeView.show(targetVC: self)
        
    }
    
    
    
    //添加子控制器
    private func addChildViewController() {
        
        addChildViewController(LBMHomeTableViewController(), title: "首页", imageName: "tabbar_home",index: 0)
        
        addChildViewController(LBMMessageTableViewController(), title: "信息", imageName: "tabbar_message_center",index: 1)
        
        addChildViewController(LBMDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover",index: 2)
        
        addChildViewController(LBMProfileTableViewController(), title: "我的", imageName: "tabbar_profile",index: 3)
        
    }
    
    
    
    private func addChildViewController(vc: UIViewController,title: String,imageName: String ,index: Int) {
        
        
        //        vc.tabBarItem.title = title
        //        vc.navigationItem.title = title
        
        vc.title = title
        
        vc.tabBarItem.tag = index
        vc.tabBarItem.image = UIImage(named: imageName)
        
        //选中时的图片
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        
        
        //统一设置标签中的字体颜色
        //        tabBar.tintColor = UIColor.orangeColor()
        
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        
        //让tabbar上的文字向上移动点
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
        
        
        
        let nav = LBMBaseNavController(rootViewController: vc)
        
        
        addChildViewController(nav)
        
    }
    
    
    //实现一个协议方法,用来添加tabbar上按钮的点击动画效果
    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        
        //        print(item.tag)
        var index = 0
        //遍历tabbar的子视图
        for v in tabBar.subviews {
            
            if v.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                if index == item.tag {
                    
                    for sub in v.subviews {
                        
                        if sub.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!) {
                            
                            sub.transform = CGAffineTransformMakeScale(0.5, 0.5)
                            
                            UIView.animateWithDuration(0.6, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 4, options: [], animations: {
                                sub.transform = CGAffineTransformIdentity
                                
                                }, completion: { (_) in
                                    
//                                    print("ok")
                            })
                        }
                        
                    }
                    
                }
                
                index+=1
                
            }
            
        }
        
    }
    
    
}
